package com.wuchao.util;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author wuchao66
 * @Date 2023/10/11 9:41
 * @PackageName:com.wuchao.util
 * @ClassName: StringToConvert1
 * @Description: TODO
 * @Param
 * @Version 1.0
 */
//全局低耦合 可以不一定是yyyy-MM-dd HH:mm:ss
public class StringToConvert1 implements Converter<String, Date> {
    //String time="2020-09-06"
    //构造入参 spring format:yyyy-MM-dd
    private String format;//你自己等会想要的格式
    public StringToConvert1(String format){
        this.format=format;
    }
    @Override
    public Date convert(String s) {
        //时间格式写出来
        DateFormat df=new SimpleDateFormat(this.format);
        Date date=null;
        try {
            date=   df.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
